package com.lhkj.ct.framework.config;

import com.lhkj.ct.base.buffer.Buffer;
import com.lhkj.ct.base.spring.SpringContextHolder;
import com.lhkj.ct.base.utils.EnvironmentUtils;
import com.lhkj.ct.framework.interceptors.BindUnitInterceptor;
import com.lhkj.ct.framework.interceptors.WxMpInterceptor;
import com.lhkj.ct.framework.log.buffer.DefaultBuffer;
import com.lhkj.ct.framework.log.processor.OperationLogProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.env.Environment;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @Description: springboot mvc配置
 * @Author:
 * @Date: 2020/6/5 12:58
 */

@Configuration
public class MvcConfig implements WebMvcConfigurer {

//    @Resource
//    ConfigurableBeanFactory beanFactory;

    @Resource
    private Environment environment;

    @Bean
    SpringContextHolder springContextHolder() {
        return new SpringContextHolder();
    }

    @Bean
    public WxMpInterceptor getWxMpInterceptor() {
        return new WxMpInterceptor();
    }

    @Bean
    public BindUnitInterceptor getBindUnitInterceptor() {
        return new BindUnitInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 量表分配记录扫码拦截器
        registry.addInterceptor(getWxMpInterceptor())
                .addPathPatterns("/api/wechat/scan/**");

        // 扫码绑定单位拦截器
        registry.addInterceptor(getBindUnitInterceptor())
                .addPathPatterns("/api/plat/patient/manage/scan/consolidation");
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory(new StringToEnumConverterFactory());
    }

    @DependsOn("springContextHolder")
    @Bean(destroyMethod = "close")
    public Buffer<String> getBuffer() {
        int size = EnvironmentUtils.resolveInt(environment, "operation-log.buffer.size", 10);
        long frequency = EnvironmentUtils.resolveLong(environment, "operation-log.buffer.frequency", 30L);
        return new DefaultBuffer(size, frequency, new OperationLogProcessor());
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 当访问/file下的资源时，会到/root/myFile/下去找
        // 例如访问：http://localhost:8080/file/1.png时会去找/root/myFile/1.png
        /** 本地文件上传路径,如果用此方式，注意中文文件名问题，这里采用nginx的代码方式 */
//        registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**")
//                .addResourceLocations("file:" + FileServerConfig.getProfile() + "/");
//        registry.addResourceHandler("/file/**").addResourceLocations("file:/usr/file/");
        // 配置knife4j静态资源
        registry.addResourceHandler("doc.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/druid/login.html").addResourceLocations(
                "classpath:/META-INF/resources/");
    }

    /**
     * 跨域支持
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//设置允许跨域的路径
                .allowedOriginPatterns("*")//设置允许跨域请求的域名
                .allowCredentials(true)//是否允许证书 不再默认开启
                .allowedMethods("GET", "POST", "PUT", "DELETE")//设置允许的方法
                .maxAge(3600);//跨域允许时间
    }

    @Bean
    public RequestContextListener requestContextListener(){
        return new RequestContextListener();
    }

}
